<template>
  <div ref="chartDiv" :style="currStyle"></div>
</template>

<script setup>
import {ref, watch, onMounted, nextTick, markRaw} from 'vue';
import * as echarts from "echarts";
import visual from "@/visual/js/visual";
import JSON5 from 'json5';
import { parseColors } from "@/utils/color";

const props = defineProps({
  node: Object,
  compStyle: Object,
  compData: Object
});
const currNode = ref(props.node);
const chartDiv = ref();
const currChart = ref(null);
const currStyle = ref();

const init = async() => {
  if (currNode.value.compData.dataSourceType === '') {
    currNode.value.compData.dataSourceType = '静态数据';
  }
  const compStyle = props.compStyle || {};
  let style = '';
  style += visual.parseTpl('width: {value};', compStyle['width']);
  style += visual.parseTpl('height: {value};', compStyle['height']);
  currStyle.value = style;

  const padding = (compStyle['padding'] || '50px,20px,30px,20px').split(',');
  let defaultOption = {};
  try {
    defaultOption = JSON5.parse(compStyle['jsonOption'] || '{}');
  } catch (e) {
    console.log("jsonOption解释出错", e)
  }
  const dataList = (await visual.loadData(props.compData, {}, {}, currNode.value)).data || [];
  currNode.value.compData.dataList = JSON.stringify(dataList);

  const seriesData = [];
  dataList.forEach(data => {
    seriesData.push({
      name: data['标签'],
      value: data['系列'],
    });
  });

  const option = {
    animation: compStyle['animation'] || false,
    color: parseColors(compStyle['colorScheme']),
    grid: {
      top: padding[0],
      right: padding[1],
      bottom: padding[2],
      left: padding[3],
      containLabel: true
    },
    series: [
      {
        ...parseGauge(compStyle),
        data: seriesData
      }
    ]
  };

  await nextTick(() => {
    try {
      if (currChart.value) {
        echarts.dispose(chartDiv.value);
      }
      currChart.value = markRaw(echarts.init(chartDiv.value));
      currChart.value.setOption(Object.assign({}, option, defaultOption));
    } catch (e) {
      console.log(e);
    }
  });
  await nextTick(() => currChart.value.resize());
};

const parseGauge = (compStyle) => {
  const result = { type: 'gauge' };

  result['radius'] = compStyle['series.radius'] || '75%';
  result['startAngle'] = compStyle['series.startAngle'] || 225;
  result['endAngle'] = compStyle['series.endAngle'] || -45;
  result['min'] = compStyle['series.minAngle'] || 0;
  result['max'] = compStyle['series.maxAngle'] || 100;
  result['splitNumber'] = compStyle['series.splitNumber'] || 10;

  result['axisLine'] = { show: !!compStyle['series.axisLine'] };
  result['axisLine'].lineStyle = {};
  result['axisLine'].lineStyle.width = compStyle['series.axisLine.width'] || 10;
  if (compStyle['series.axisLine.color']) {
    const colorList = [];
    const colors = compStyle['series.axisLine.color'].split(',');
    for (let i=0; i<colors.length; i=i+2) {
      colorList.push([colors[i], colors[i+1]]);
    }
    result['axisLine'].lineStyle.color = colorList;
  }

  result['progress'] = {
    show: !!compStyle['series.progress'],
    width: compStyle['series.progress.width'] || 10
  };

  result['splitLine'] = {
    show: !!compStyle['series.splitLine'],
    distance: compStyle['series.splitLine.distance'] || 10,
    lineStyle: {
      color: compStyle['series.splitLine.color'] || '#63677A'
    }
  };

  result['axisTick'] = {
    show: !!compStyle['series.axisTick'],
    distance: compStyle['series.axisTick.distance'] || 10,
    lineStyle: {
      color: compStyle['series.axisTick.color'] || '#63677A'
    },
  };

  result['axisLabel'] = { show: !!compStyle['series.axisLabel'] };
  result['pointer'] = {
    show: !!compStyle['series.pointer'],
    itemStyle: {
      color: compStyle['series.pointer.color'] || 'auto'
    }
  };
  result['title'] = {
    show: !!compStyle['series.title'],
    offsetCenter: (compStyle['series.title.offset'] || '0%,20%').split(',')
  };

  result['detail'] = {
    show: !!compStyle['series.detail'],
    formatter: compStyle['series.detail.formatter'],
    fontSize: compStyle['series.detail.fontSize'] || 14,
    color: compStyle['series.detail.color'] || 'inherit'
  };

  return result;
};

watch(() => props.compStyle, () => {
  init();
}, { deep: true });

watch(() => props.compData?.refresh, () => {
  init();
});

onMounted(() => {
  init();
});
</script>